--- title: Title keywords: fastai sidebar: home_sidebar summary: "summary" ---
import math
import commons.M100_Fluke_dirs as dirs
import pandas
import  library.inference_utils as iu

from fastai.vision import *
from torchsummary import summary

%reload_ext autoreload
%autoreload 2
%matplotlib inline

Run model inference on all of the 83 Test tiles

dirs.set_base_path('/home/john/github/data/FEC/03-M100-Fluke-2019-11')
dirs.model = dirs.model+'/export-fluke-2019-12-01.pkl'
dirs.printdirs(dir(dirs))
 Directorises for Fluke preparatioan and training and testing images

  basepath        :  /home/john/github/data/FEC/03-M100-Fluke-2019-11                                    
  crop            :  /home/john/github/data/FEC/03-M100-Fluke-2019-11/Crop-200                           
  cropLabel       :  /home/john/github/data/FEC/03-M100-Fluke-2019-11/Crop-200/Label                     
  cropTrain       :  /home/john/github/data/FEC/03-M100-Fluke-2019-11/Crop-200/Train                     
  cropValidTxtFile:  /home/john/github/data/FEC/03-M100-Fluke-2019-11/Crop-200/valid.txt                 
  label           :  /home/john/github/data/FEC/03-M100-Fluke-2019-11/Fullsize/Label                     
  model           :  /home/john/github/data/FEC/03-M100-Fluke-2019-11/models//export-fluke-2019-12-01.pkl
  originImages    :  /home/john/github/data/FEC/03-M100-Fluke-2019-11/Original                           
  test            :  /home/john/github/data/FEC/03-M100-Fluke-2019-11/Fullsize/Test                      
  train           :  /home/john/github/data/FEC/03-M100-Fluke-2019-11/Fullsize/Train                     
  validTxtFile    :  /home/john/github/data/FEC/03-M100-Fluke-2019-11/Fullsize/valid.txt                 
path = Path(dirs.basepath)/'Crop-200'
path_img = path/'Train'
path_lbl = path/'Label'
path_tst = path/'Test'

fnames =  get_image_files(path_tst)

print (f'Number of test tiles {len(fnames)}')
Number of test tiles 83

Load exported learner

# defaults.device = 'cpu'
defaults.device = 'cuda'

def acc_metric1(input, target):
    target = target.squeeze(1)
    return (input.argmax(dim=1)==target).float().mean()

def acc_metric2(input, target):
    target = target.squeeze(1)
    return (input.argmax(dim=1)[target>0]==target[target>0]).float().mean()
    
learn = load_learner('', dirs.model)

learn.model.float()
# summary(learn.model, (3, 32, 32))
print("Learner loaded")
Learner loaded
fnames =  sorted(get_image_files(path/'Test'))

# preds= iu.run_inferences(learn, fnames, start=60)
preds= iu.run_inferences(learn, fnames, number_files='all')
len(preds)
83
# print(f'Creating dataframe in path {path_tst}')
# 
# fnames = sorted(get_image_files(path_tst))
# df = iu.create_probs_df(fnames)
# df.tail(10)
print(f'Populate dataframe in path {path_tst}')
df = pd.read_csv(path/"test_df.csv")

lst = iu.add_cols_to_probs_df(df)
df.sort_values(by=['Name'])
df.to_csv(path/"test_df.csv")
df.tail(10)

print(f'Adding inference probs to the dataframe')
iu.probs_to_df(preds, df)   
df.to_csv(dirs.crop+'/results_df.csv', index=False)
df.sort_values(by=['Name'], inplace=True)

print(f'Find Maximums')
df = pd.read_csv(path/"results_df.csv")
df["max_prob"] = df[["Background", "Fluke_Liver", "Fluke_Rumen"]].max(axis=1)
df.tail(10)
df.loc[df.Fluke_Liver == df.max_prob, "max_prob"] = 'Fluke-Liver'
df.loc[df.Fluke_Rumen == df.max_prob, "max_prob"] = 'Fluke-Rumen'
df.tail(10)
df.to_csv(dirs.crop+'/results_df.csv', index=False)
Populate dataframe in path /home/john/github/data/FEC/03-M100-Fluke-2019-11/Crop-200/Test
Adding inference probs to the dataframe
/home/john/github/techion_expts/library/inference_utils.py:165: FutureWarning: Using a non-tuple sequence for multidimensional indexing is deprecated; use `arr[tuple(seq)]` instead of `arr[seq]`. In the future this will be interpreted as an array index, `arr[np.array(seq)]`, which will result either in an error or a different result.
  mask[AAcoords.T.tolist()] = True
Find Maximums
df.tail(10)
Unnamed: 0 Name Label Op Background Fluke_Liver Fluke_Rumen Other Pstr max_prob
73 73 236571 - 3-0.jpg Fluke-Rumen Test 0.20 0.00 0.79 0 B:0.20 L:0.00 R:0.79 Fluke-Rumen
74 74 236568 - 46-0.jpg Fluke-Liver Test 0.15 0.85 0.00 0 B:0.15 L:0.85 R:0.00 Fluke-Liver
75 75 235444 - 11-0.jpg Fluke-Liver Test 0.18 0.82 0.00 0 B:0.18 L:0.82 R:0.00 Fluke-Liver
76 76 235444 - 11-1.jpg Fluke-Liver Test 0.17 0.83 0.00 0 B:0.17 L:0.83 R:0.00 Fluke-Liver
77 77 235444 - 2-0.jpg Fluke-Liver Test 0.30 0.69 0.01 0 B:0.30 L:0.69 R:0.01 Fluke-Liver
78 78 235444 - 2-1.jpg Fluke-Liver Test 0.17 0.83 0.00 0 B:0.17 L:0.83 R:0.00 Fluke-Liver
79 79 236568 - 4-0.jpg Fluke-Liver Test 0.16 0.84 0.00 0 B:0.16 L:0.84 R:0.00 Fluke-Liver
80 80 236568 - 4-1.jpg Fluke-Liver Test 0.15 0.85 0.00 0 B:0.15 L:0.85 R:0.00 Fluke-Liver
81 81 235445 - 15-0.jpg Fluke-Liver Test 0.15 0.85 0.00 0 B:0.15 L:0.85 R:0.00 Fluke-Liver
82 82 235445 - 15-1.jpg Fluke-Liver Test 0.15 0.85 0.00 0 B:0.15 L:0.85 R:0.00 Fluke-Liver
iu.plot_inferences(preds, df, src_path=path_tst, start=0)
# add the actual label   todo
iu.plot_inferences(preds, df, src_path=path_tst, start=20)
# add the actual label   todo
iu.plot_inferences(preds, df, src_path=path_tst, start=40)
# add the actual label   todo
iu.plot_inferences(preds, df, src_path=path_tst, start=60)
# add the actual label   todo
iu.plot_inferences(preds, df, src_path=path_tst, start=80)
# add the actual label   todo
preds[-4]['filename']
'236571 - 25-0.jpg'
len(preds)
83
fnames =  sorted(get_image_files(path/'Test'))
fn = fnames[65]
img = open_image(fn)
pc,pi,raw_pred = learn.predict(img)

p = iu.calc_probs(raw_pred)
print(f'{p[0]:3.2f} {p[1]:3.2f} {p[2]:3.2f}')
#     PIL.Image.fromarray(img.astype(np.uint8)).save(fn, quality=90)
# print(f'Label files: {len(fnames)} resaved')

pc
0.13 0.54 0.33
/home/john/github/techion_expts/library/inference_utils.py:102: FutureWarning: Using a non-tuple sequence for multidimensional indexing is deprecated; use `arr[tuple(seq)]` instead of `arr[seq]`. In the future this will be interpreted as an array index, `arr[np.array(seq)]`, which will result either in an error or a different result.
  mask[AAcoords.T.tolist()] = True
# data = pd.read_csv('https://s3-eu-west-1.amazonaws.com/shanebucket/downloads/uk-500.csv')
# 
# data.set_index("last_name", inplace=True)
# data.head()
# data.head(5)
first_name company_name address city county postal phone1 phone2 email web
last_name
Tomkiewicz Aleshia Alan D Rosenburg Cpa Pc 14 Taylor St St. Stephens Ward Kent CT2 7PP 01835-703597 01944-369967 atomkiewicz@hotmail.com http://www.alandrosenburgcpapc.co.uk
Zigomalas Evan Cap Gemini America 5 Binney St Abbey Ward Buckinghamshire HP11 2AX 01937-864715 01714-737668 evan.zigomalas@gmail.com http://www.capgeminiamerica.co.uk
Andrade France Elliott, John W Esq 8 Moor Place East Southbourne and Tuckton W Bournemouth BH6 3BE 01347-368222 01935-821636 france.andrade@hotmail.com http://www.elliottjohnwesq.co.uk
Mcwalters Ulysses Mcmahan, Ben L 505 Exeter Rd Hawerby cum Beesby Lincolnshire DN36 5RP 01912-771311 01302-601380 ulysses@hotmail.com http://www.mcmahanbenl.co.uk
Veness Tyisha Champagne Room 5396 Forth Street Greets Green and Lyng Ward West Midlands B70 9DT 01547-429341 01290-367248 tyisha.veness@hotmail.com http://www.champagneroom.co.uk
fn = fnames[64]
img = open_image(fn)
pc,pi,o = learn.predict(img)

fig, axes = plt.subplots(1, 4, figsize=(10, 3))

for i, ax in enumerate(axes.flat):
    arr = (o[i,:,:]).numpy()
    im = ax.imshow(arr, cmap='inferno', vmin=0, vmax=1)
    ax.set_axis_off()
    # plt.colorbar(im,ax=ax)

plt.tight_layout()

o.shape
# 
torch.Size([4, 200, 200])
from skimage.measure import label, regionprops

def _get_props(tens, layer, min_conf):
    min_area = 100
    arr = o[layer,:,:].numpy()
    if layer == 0:
        label_arr = label(arr < 1-min_conf)
    else:
        label_arr = label(arr > min_conf)
        
    region_props = []
    for region in regionprops(label_arr, arr, cache=True):
        if region.area > min_area:
            cx = int(region.centroid[1])
            cy = int(region.centroid[0])
            region_props.append({ 
                "class_layer": layer,
                "centroid": region.centroid, 
                "mean_intensity": region.mean_intensity,
                "area": region.area,
                "coords": region.coords,
            })     
    return region_props
def _get_bool_mask(o):
    min_conf = 0.5
    arr = o[0,:,:].numpy()
    mask = np.zeros((arr.shape), dtype=bool)
    bgnd = get_props(o, 0, min_conf)
    AAcoords = bgnd[0]["coords"]
    mask[AAcoords.T.tolist()] = True
    return mask
def _calc_probs(o):
    mask = get_bool_mask(o)
    p = [None]*o.shape[0]
    for i in range(o.shape[0]):
        p[i] = o[i,:,:].numpy()[mask].mean()
        
    return p

Calculate probability of marked up region

# get boolean mask of a region
mask = get_bool_mask(o)
show_img(mask)
/home/john/.virtualenvs/cv/lib/python3.6/site-packages/ipykernel_launcher.py:7: FutureWarning: Using a non-tuple sequence for multidimensional indexing is deprecated; use `arr[tuple(seq)]` instead of `arr[seq]`. In the future this will be interpreted as an array index, `arr[np.array(seq)]`, which will result either in an error or a different result.
  import sys
<matplotlib.axes._subplots.AxesSubplot at 0x7faf11635ef0>
p = calc_probs(o)
print(f'{p[0]:3.2f} {p[1]:3.2f} {p[2]:3.2f}')

p
# print(f'{p:3.2f}')p
# print(f'{p0.mean():3.2f},{p1.mean():3.2f},{p2.mean():3.2f},{p3.mean():3.2f}')
# print(f'{(p0+p1+p2).mean():3.2f}')
0.10 0.02 0.88
/home/john/.virtualenvs/cv/lib/python3.6/site-packages/ipykernel_launcher.py:7: FutureWarning: Using a non-tuple sequence for multidimensional indexing is deprecated; use `arr[tuple(seq)]` instead of `arr[seq]`. In the future this will be interpreted as an array index, `arr[np.array(seq)]`, which will result either in an error or a different result.
  import sys
[0.096528605, 0.020488279, 0.88295305, 3.0051324e-05]
min_conf = 0.2
bgnd = get_props(o, 0, min_conf)
lyr1 = get_props(o, 1, min_conf)
lyr1
lyr2 = get_props(o, 2, min_conf)
lyr2
print(bgnd[0]["area"] )
print(lyr1[0]["area"], lyr1[0]["mean_intensity"], lyr1[0]["mean_intensity"] * lyr1[0]["area"] / bgnd[0]["area"])
print(lyr2[0]["area"], lyr2[0]["mean_intensity"], lyr2[0]["mean_intensity"] * lyr2[0]["area"] / bgnd[0]["area"])
# region.mean_intensity * region.area / 314
bgnd[0]["centroid"] 

print(o[0,90,90], o[1,90,90], o[2,90,90], o[3,90,90])
AAcoords = bgnd[0]["coords"]
AAcoords
array([[ 82,  89],
       [ 82,  90],
       [ 82,  91],
       [ 82,  92],
       ...,
       [100,  93],
       [100,  94],
       [101,  92],
       [101,  93]])
arr = o[0,:,:].numpy()
bin = label(arr < 0.5) > 0
bin.max()
True
AA0 = o[0,:,:].numpy()[bin]
AA0.mean()
0.096528605
AA1 = o[1,:,:].numpy()[bin]
AA1.mean()
0.020488279
AA2 = o[2,:,:].numpy()[bin]
AA2.mean()
0.88295305
(AA0+AA1+AA2).mean()
0.99996996
def find_prediction_blobs(img, min_conf=0.1, min_area=100, plot=False):
    RESIZE = 2.0
    img = cv2.resize(img,None,fx=1.0/RESIZE, fy=1.0/RESIZE, interpolation = cv2.INTER_AREA)
    min_area = int(min_area/RESIZE)
    # region props seems to have region.max_intensity errors if no data not np.int 
    SCALE = 100
    min_conf *= SCALE
    img = (img * (SCALE/img.max())).astype(np.int)
    img[img[:,:,0]<min_conf,0] = 0
    img[img[:,:,1]<min_conf,1] = 0

    predictions = []


    label_image0 = label(img[:,:,0] > min_conf)
    label_image1 = label(img[:,:,1] > min_conf)

    for region in regionprops(label_image0, img[:,:,0], cache=True):
        if region.area > min_area:
            cx = int(region.centroid[1]*RESIZE)
            cy = int(region.centroid[0]*RESIZE)
            predictions.append({  
                "label": 'Strongyle', 
                "point": [cx,cy], 
                "probability": region.max_intensity.round(2),
                "area": region.area
            })  

    fill = (0,255,0)
    for region in regionprops(label_image1, img[:,:,1]):
        if region.area > min_area:
            cx = int(region.centroid[1]*RESIZE)
            cy = int(region.centroid[0]*RESIZE)
            predictions.append({
                "label": 'Nematodirus',
                "point": [cx,cy], 
                "probability": region.max_intensity.round(2),
                "area": region.area
            })
    if plot:
        #   show_img(imglab, figsize = (15,15))
        plt.figure(figsize=(15, 15))
        plt.subplot(121)
        plt.imshow(label_image0, cmap='nipy_spectral')
        #   plt.imshow(img[:,:,0] > min_conf, cmap='nipy_spectral')
        plt.axis('off')
        plt.subplot(122)
        plt.imshow(label_image1, cmap='nipy_spectral')
        plt.axis('off')

        plt.tight_layout()
        plt.show()

    return predictions, img